package com.group.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.group.config.Async;
import com.group.entity.ProjectOutside;
import com.group.entity.User;
import com.group.mapper.ProjectOutsideMapper;
import com.group.mapper.UserMapper;
import com.group.service.ProjectOutsideService;
import com.group.service.UserService;
import com.group.vo.DataVO;
import com.group.vo.ProjectVO;
import com.group.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@SuppressWarnings("all")
@RequestMapping("/admin")
@CrossOrigin
public class ManageMentController {
    @Autowired
    UserMapper userMapper;
    @Autowired
    Async async;
    @Autowired
    ProjectOutsideMapper projectOutsideMapper;
    @Autowired
    ProjectOutsideService projectOutsideService;

    @Autowired
    UserService userService;



    //通过projectId 来封禁项目，前端需要传projectId
    @GetMapping("/banProjectId/{projectId}")
    public void banProjectId(@PathVariable("projectId") Integer projectId) {
        //逻辑删除,status == 1 表示逻辑删除，除了后台都查不到，查询不到，无法登陆
        projectOutsideMapper.deleteById(projectId);
    }

    //封禁用户接口
    @PostMapping("/banId")
    @ResponseBody
    public int banId(@RequestParam(value = "id", required = true) Integer id) {
        //逻辑删除，查询不到，无法登陆
        int i = userMapper.deleteById(id);
        return i;
    }


    //查询用户
    @ResponseBody
    @RequestMapping("/userList")
    public DataVO list(Integer page, Integer limit) {
        return userService.findData(page, limit);
    }

    //更新用户
    @PostMapping("/updateUser")
    @ResponseBody
    public int updateUser(@RequestBody UserVO userVO) {
        User user = new User();
        BeanUtils.copyProperties(userVO, user);
        int i = userMapper.updateById(user);
        return i;
    }

    //添加用户
    @PostMapping("/addUser")
    @ResponseBody
    public boolean addUser(@RequestBody UserVO userVO) {
        User user = new User();
        user.setUserName(userVO.getUserName());
        user.setPassWord(userVO.getPassWord());
        user.setAuthority(userVO.getAuthority());
        user.setPersonalName(userVO.getPersonalName());
        user.setCourseVip(userVO.getCourseVip());
        boolean save = userService.save(user);
        return save;
    }

    @ResponseBody
    @RequestMapping("/projectList")
    public DataVO projectList(Integer page, Integer limit) {
        return projectOutsideService.findData(page, limit);
    }

    @PostMapping("/banProjectById")
    @ResponseBody
    public int banProjectById(@RequestParam(value = "id", required = true) Integer id) {
        //逻辑删除，查询不到，无法登陆
        int i = projectOutsideMapper.deleteById(id);
        return i;
    }

    //更新项目
    @PostMapping("/updateProject")
    @ResponseBody
    public int updateProject(@RequestBody ProjectVO projectVO) {
        ProjectOutside projectOutside = new ProjectOutside();
        BeanUtils.copyProperties(projectVO, projectOutside);
        int i = projectOutsideMapper.updateById(projectOutside);
        return i;
    }


}
